package com.hxd.otc.module.order.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.dialog.ConfirmDeleteDialog;
import com.hxd.otc.module.order.adapter.ComplainListAdapter;
import com.hxd.otc.module.order.bean.AppealBean;
import com.hxd.otc.module.order.contract.MyApplealContract;
import com.hxd.otc.module.order.presenter.MyAppleaPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;

public class ComplainListActivity extends BaseActivity<MyAppleaPresenter> implements MyApplealContract.View {


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    View errorView;

    View notDataView;

    TextView tvRefresh;

    ComplainListAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_complain_list;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.doLoadData(true);
            }
        });

        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRefreshLayout.autoRefresh(0, 10, 1);
            }
        });
    }

    ConfirmDeleteDialog dialog;

    private void cancelAppeal() {
        if (dialog == null)
            dialog = new ConfirmDeleteDialog(this)
                    .setTitleText(R.string.is_cancel_appeal)
                    .setOnConfirmClickListener(new ConfirmDeleteDialog.OnConfirmClickListener() {
                        @Override
                        public void onConfirm() {
                            mPresenter.cancleAppeal(cancleId);
                            dialog.dismiss();
                        }
                    });

        dialog.show();
    }

    String cancleId;

    private void initAdapter() {
        if (adapter == null) {
            adapter = new ComplainListAdapter();
            adapter.setLinster(new ComplainListAdapter.OnClickCancleLinster() {
                @Override
                public void onCancle(String id) {
                    cancleId = id;
                    cancelAppeal();
                }
            });
        }
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        initAdapter();
        mRefreshLayout.autoRefresh(0, 10, 1);
    }


    @Override
    protected void initEvent() {

    }

    @Override
    protected MyAppleaPresenter createPresenter() {
        return new MyAppleaPresenter(this);
    }


    @Override
    public void onSetAdapter(List<AppealBean> list) {

        if (list != null && list.size() <= 0) {
            adapter.setEmptyView(notDataView);
        }
        adapter.setNewData(list);
    }

    @Override
    public void onShowNoMore() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    @Override
    public void cancleSuccedd() {
        showToast(getString(R.string.cancel_appeal_succed));
        mPresenter.doLoadData(true);
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        adapter.setEmptyView(errorView);
    }
}
